package com.lqq.controller.restfull;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.lqq.entity.User;
import com.lqq.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lqq
 * @date 2019/1/6
 */
@RestController
@Api(description = "动态sql:trim|where|set|foreach|if|choose|when|otherwise|bind")
public class DynamicSqlController {

    @Autowired
    private UserService userService;

    @RequestMapping("/forEach")
    @ApiOperation(value = "forEach",httpMethod = "POST")
    public String forEach(@RequestParam @ApiParam(required = true) Long[] ids){
        userService.updateBatch(ids,1);
        return "ok";
    }

    //TODO 用单独的 "翠" 模糊查询不出来
    @RequestMapping("/if")
    @ApiOperation(value = "if",httpMethod = "POST")
    public PageInfo<User> iF(@RequestParam @ApiParam(required = true,defaultValue = "0") Integer status,
                             @RequestParam(required = false) @ApiParam(required = false,example = "翠")String name){
        return userService.findByStatusAndNameLike(status,name);
    }


    @RequestMapping("/chooseWhenOtherwise")
    @ApiOperation(value = "chooseWhenOtherwise",httpMethod = "POST")
    public PageInfo<User> chooseWhenOtherwise(@RequestParam(required = false) @ApiParam(required = false,example = "0") Integer status,
                             @RequestParam(required = false) @ApiParam(required = false,example = "翠")String name){
        return userService.chooseWhenOtherwise(status,name);
    }


//    TODO trim , where ,set  待补充
    /**
     *  trim 可以用来解决and 或者连接是最后一个多一个逗号","的问题，
     *  set： 用于更新数据库中只想更新的字段
     *  where: 避免if判断的所有条件都没有达到的时候sql语句后面跟一个where。
     *
     */
}
